package dbobj

import (
	"iot-base/common/dbconn"
	"strings"
)

type PickTemplate struct {
	//ID           int    `gorm:"primaryKey;AUTO_INCREMENT;not null" json:"id"`
	SName              string   `gorm:"primaryKey;" json:"s_name" redis:"SName"`
	AlarmDisabled      string   `json:"alarm_disabled" redis:"AlarmDisabled"`
	PickDisabled       string   `json:"pick_disabled" redis:"PickDisabled"`
	AlarmDisabledArray []string `gorm:"-" sql:"-" `
	PickDisabledArray  []string `gorm:"-" sql:"-"`
}

type DevPickTemplate struct {
	ThingID          string        `gorm:"primaryKey;not null" json:"thing_id"`
	PickTemplateName string        `json:"pick_template_name"`
	PickTemplatePtr  *PickTemplate `gorm:"foreignKey:PickTemplateName" json:"-" mapstructure:"-"`
}

func init() {
	dbconn.DBConnection.AutoMigrate(&PickTemplate{})
	dbconn.DBConnection.AutoMigrate(&DevPickTemplate{})
}

func (pt *PickTemplate) IsAlarmEnable(pid string) bool {
	if pt.AlarmDisabled == "" {
		return true
	}
	if pid != "" {
		if pt.AlarmDisabledArray == nil {
			pt.AlarmDisabledArray = strings.Split(pt.AlarmDisabled, ",")
		}
		for _, id := range pt.AlarmDisabledArray {
			if id == pid {
				return false
			}
		}
	}
	return true

}

func (pt *PickTemplate) IsPickEnable(pid string) bool {
	if pt.PickDisabled == "" {
		return true
	}
	if pid != "" {
		if pt.PickDisabledArray == nil {
			pt.PickDisabledArray = strings.Split(pt.PickDisabled, ",")
		}
		for _, id := range pt.PickDisabledArray {
			if id == pid {
				return false
			}
		}
	}
	return true

}
